package com.farmUni.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.farmUni.common.BaseContext;
import com.farmUni.entity.Article;
import com.farmUni.entity.User;
import com.farmUni.entity.Vo.UserShowVO;
import com.farmUni.mapper.UserMapper;
import com.farmUni.service.ArticleService;
import com.farmUni.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmUni.utils.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author farm
 * @since 2024-02-07
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private ArticleService articleService;

    @Override
    public String userLogin(User user) {
        String openid = user.getOpenid();
        if(user.getUsername() != null || !StringUtils.isBlank(user.getUsername())) {
            User one = this.getOne(Wrappers.<User>lambdaQuery()
                    .eq(User::getUsername, user.getUsername())
                    .last("LIMIT 1"));
            one.setOpenid(openid);
            Long userId = one.getUserId();
            return AppJwtUtil.getToken(userId);
        }
        User byId = this.getById(openid);
        if (byId == null) {
            this.save(user);
        }
        return AppJwtUtil.getToken(user.getUserId());
    }

    @Override
    public String userLogin2(User user) {
        String openid = user.getOpenid();
        User one = this.getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getOpenid, openid)
                .last("LIMIT 1"));
        if (one == null) {
            return null;
        }
        return AppJwtUtil.getToken(one.getUserId());
    }

    @Override
    public User getDetail() {
//        String openid = BaseContext.getCurrentOpenId();
        Long currentId = BaseContext.getCurrentId();
        return this.getById(currentId);
    }

    @Override
    public UserShowVO getDetail2() {
        //String openid = BaseContext.getCurrentOpenId();
        Long currentId = BaseContext.getCurrentId();
        UserShowVO userShowVO = new UserShowVO();
        User user = this.getOne(Wrappers.<User>lambdaQuery().eq(User::getUserId,currentId).last("LIMIT 1"));
        int liking = 0;
        if(user != null){
            BeanUtils.copyProperties(user,userShowVO);
            List<Article> articleList = articleService.list(Wrappers.<Article>lambdaQuery()
                    .eq(Article::getUserId, currentId));
            if(articleList != null){
                for(Article article:articleList){
                    liking += article.getLiking();
                }
            }
            userShowVO.setLiking(liking);
            return userShowVO;
        }else {
            return null;
        }
    }
}
